/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.model;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import pl.smath.expression.utils.SortProductComparator;


public class ProductTerm extends MultiTerm{

	/**
	 * constructor
	 */
	public ProductTerm() {
		super("*");
	}


	/**
	 * @return tekst wyrażenia
	 */
	public Term copy(){
		ProductTerm term = new ProductTerm();
		
		for(int i = 0; i < getTermsList().size(); i++){
			term.addTerm(getTermsList().get(i).copy());
		}
		
		term.setNegative(isNegative());
		return term;
	}

	
	/**
	 * Porówanie wyrazen
	 */
	public boolean isTheSame(Term term){
		
		if(!(term instanceof ProductTerm))
			return false;
		
		return super.isTheSame(term);
	}
	
	/**
	 * Uporzadkowanie minusow
	 */
	@Override
	public void normalizeSign(){
	
		boolean negative = false;
		for(Term term : getTermsList()){
			if(term.isNegative()){
				negative = !negative;
				term.setNegative(false);
			}
		}
		
		setNegative(isNegative() != negative);

	}
	
	
	/**
	 * Usunięcie wewnetrznych nawiasów
	 * @return nowe wyrazenie lub to samo, jezeli nie ma wewnetrznych nawiasow
	 */
	@Override
	public Term removeInnerBrackets() {

		List<Term> terms = getTermsList();
		List<Term> outputTerms = new Vector<Term>();
		Term outputTerm = this;
		boolean found = false;
		boolean negative = false;
		
		for(Term term : terms){
		
			if(term instanceof ProductTerm){
				
				found = true;
				negative = (negative != term.isNegative());
				for(Term insideTerm : ((ProductTerm)term).getTermsList()){
					Term copyTerm = insideTerm.copy();
					copyTerm.setNegative(term.isNegative() != insideTerm.isNegative());
					outputTerms.add(copyTerm);
				}
			}
			else{
				outputTerms.add(term);
			}
		}
		
		
		if(found){
			outputTerm = new ProductTerm();
			outputTerm.setNegative(isNegative() != negative);
			((ProductTerm)outputTerm).addTerms(outputTerms);
			outputTerm.normalizeSign();
		}
		
		return outputTerm;
	}

	
	/**
	 * Proste sortowanie. Może wystarczy?
	 */
	@Override
	public void sort() {
		sortTerms(new SortProductComparator());
	}

	
	/**
	 * Zamiana do stringa
	 */
	public String toString(){
		String output = "";
		Iterator<Term> terms = getTerms();
		
		while(terms.hasNext()){
			Term term = terms.next();
			
			if(output.length() > 0){
				output += "*";
			}

			if(term instanceof SumTerm)
				output += "(" + term.toString() + ")";
			else
				output += term.toString();
		}
		
		if(isNegative())
			output = "-" + output;
		
		return output;
	}


	/**
	 * Transformacja wezla
	 */
	@Override
	public Term transform(ITransform t) {
		Term outputTerm = null;
	
	    outputTerm = t.process(this);
	    
	    if(outputTerm == this){
	
	    	Vector<Term> newTerms = new Vector<Term>();
	    	boolean		modified = false;
	    	
	    	for(Term term : getTermsList()){
	    		Term newTerm = term.transform(t);
	    		newTerms.add(newTerm);
	    		
	    		if(newTerm != term){
	    			modified = true;
	    		}
	    	}
	    	
	    	if(modified){
	    		ProductTerm productTerm = new ProductTerm();
	    		productTerm.setNegative(isNegative());
	    		productTerm.addTerms(newTerms);
	    		outputTerm = productTerm.removeInnerBrackets();
	    	}
	    }
	    else{
	    	outputTerm = outputTerm.transform(t);
	    }
	    
	    return outputTerm;
	}
	
}
